package com.hbue.water.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hbue.water.entity.Salary;
import com.hbue.water.entity.Worker;
import com.hbue.water.mapper.SalaryMapper;
import com.hbue.water.service.SalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yushu
 * @date 2021/12/26
 * @apiNate
 */
@Service
public class SalaryServiceImpl implements SalaryService {

    @Autowired
    private SalaryMapper salaryMapper;

    @Override
    public PageInfo<Salary> listSalary(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Salary> salaries = this.calcSalary();
        return new PageInfo<>(salaries);
    }

    /**
     * 计算每个送水工的工资
     *
     * @return 送水工工资列表
     */
    @Override
    public List<Salary> calcSalary() {
        return salaryMapper.calcSalary();
    }

    /**
     * 搜索某一时间段的送水工工资
     *
     * @param beginDate 开始时间
     * @param endDate   结束时间
     * @return 满足条件的送水工工资列表
     */
    @Override
    public List<Salary> searchSalaryByCondition(String beginDate, String endDate) {
        // 条件成立：“结束时间”为空，将系统当前时间设置为结束时间
        if (StrUtil.isEmpty(endDate)) {
            LocalDate date = LocalDate.now();
            DateTimeFormatter dt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            endDate = dt.format(date);
        }
        // 查询满足条件的已送水的送水工信息
        List<Salary> salaryList = salaryMapper.searchSalaryByCondition(beginDate, endDate);
        // 查询不满足条件的送水工信息
        List<Worker> workerList = salaryMapper.searchNonSalaryByCondition(beginDate, endDate);

        // 在salaryList集合对象中提取所有送水工名称
        List<Integer> workerIdList = salaryList.stream()
                .map(Salary::getWid)
                .collect(Collectors.toList());
        // 遍历workerNameList集合里面的数据，如果集合中的数据在salaryList里面不存在，将其放入salaryList集合
        workerList.forEach(worker -> {
            // 条件成立：表示workerList集合的数据在workerNameList集合中不存在
            // 将worker添加salaryList集合
            if (!workerIdList.contains(worker.getWid())) {
                Salary sa = new Salary();
                sa.setWorkerName(worker.getWorkerName());
                sa.setWorkerMoney(worker.getWorkerMoney());
                sa.setWorkerSalary(worker.getWorkerSalary());
                // 没有在区间段送过水的送水工送水数量设置为0
                sa.setSendWaterCount(0);
                // 没有在区间段送过水的送水工实发工资就是基本工资
                sa.setFinalSalary(Double.valueOf(worker.getWorkerSalary()));
                salaryList.add(sa);
            }
        });
        // bug: 实发工资没有按照降序排序
        // 解决方案： 对实发工资使用Comparator接口进行比较
        //Collections.sort(salaryList, new Comparator<Salary>() {
        //    @Override
        //    public int compare(Salary o1, Salary o2) {
        //        if (o1.getFinalSalary() > o2.getFinalSalary()) {
        //            return -1;
        //        } else if (o1.getFinalSalary() == o2.getFinalSalary()) {
        //            return 1;
        //        } else {
        //            return 0;
        //        }
        //    }
        //});
        Collections.sort(salaryList, (o1, o2) -> {
            if (o1.getFinalSalary() > o2.getFinalSalary()) {
                return -1;
            } else if (o1.getFinalSalary().equals(o2.getFinalSalary())) {
                return 1;
            } else {
                return 0;
            }
        });
        return salaryList;
    }
}
